package com.wjf.birdfish.util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/** 
 * Created on 2006-10-28
 * <p>Title: 分页对象</p>
 * <p>Description:包含当前页数据及分页信息</p> 
 * <p>Copyright: Copyright TYDIC (c) 2006</p>
 * <p>Company: tydic.com</p>
 * @author <a href="malito:tongys@tydic.com">童永胜</a>
 * @version 1.0
 */
public class Page implements Serializable {

	private static final long serialVersionUID = 1L;

	static private int DEFAULT_PAGE_SIZE = 15;

    /**
     * 每页的记录数
     */
    private int pageSize = DEFAULT_PAGE_SIZE;

    /**
     * 当前页第一条数据在List中的位置,从0开始
     */
    private int start;
	long currPageNo = 0;
	long totalPageCount = 0;
    /**
     * 当前页中存放的记录,类型一般为List
     */
    private List data;

    /**
     * 总记录数
     */
    private long totalCount;
    
    /**
     * 构造方法，只构造空页
     */
    public Page() {
        this(0, 0, DEFAULT_PAGE_SIZE, new ArrayList());
    }

    /**
     * 默认构造方法
     *
     * @param start     本页数据在数据库中的起始位置
     * @param totalSize 数据库中总记录条数
     * @param pageSize  本页容量
     * @param data      本页包含的数据
     */
    public Page(int start, long totalSize, int pageSize, List data) {
        this.pageSize = pageSize;
        this.start = start;
        this.totalCount = totalSize;
        this.data = data;
    }

    /**
     * 取数据库中包含的总记录数
     */
    public long getTotalCount() {
        return this.totalCount;
    }

    /**
     * 取总页数
     */
    public long getTotalPageCount() {
        if (totalCount % pageSize == 0)
            return totalCount / pageSize;
        else
            return totalCount / pageSize + 1;
    }

    /**
     * 取每页数据容量
     */
    public int getPageSize() {
        return pageSize;
    }

    /**
     * 当前页中的记录
     */
    public Object getResult() {
        return data;
    }

    /**
     * 取当前页码,页码从1开始
     */
    public int getCurrentPageNo() {
        return totalCount == 0 ? 0 : start / pageSize + 1;
    }

    /**
     * 是否有下一页
     */
    public boolean hasNextPage() {
        return this.getCurrentPageNo() < this.getTotalPageCount() - 1;
    }

    /**
     * 是否有上一页
     */
    public boolean hasPreviousPage() {
        return this.getCurrentPageNo() > 1;
    }

    /**
     * 获取任一页第一条数据的位置，每页条数使用默认值
     */
    protected static int getStartOfPage(int pageNo) {
        return getStartOfPage(pageNo, DEFAULT_PAGE_SIZE);
    }

    /**
     * 获取任一页第一条数据的位置,startIndex从0开始
     */
    public static int getStartOfPage(int pageNo, int pageSize) {
        return (pageNo - 1) * pageSize;
    }

	public List getData() {
		return data;
	}

	public void setData(List data) {
		this.data = data;
	}

	public long getFirstPage() {
		return totalCount > 0 ? 1 : 0;
	}

	public long getLastPage() {
		return getTotalPageCount();
	}

	public long getNextPage() {
		if (getTotalPageCount() == 0) return 0;
		
		long currPage = getCurrentPageNo();
		if (currPage > 0) {
			currPage++;
			if (currPage > getTotalPageCount())
				currPage = getTotalPageCount();
		}
		else currPage = 0;
		return currPage;
	}

	public long getPrevPage() {
		if (getTotalPageCount() == 0) return 0;
		
		long currPage = getCurrentPageNo();
		if (currPage > 0) {
			currPage = (currPage == 1 ? 1 : currPage - 1);
		}
		else currPage = 0;
		return currPage;
	}

	public long getCurrPageNo() {
		 return totalCount == 0 ? 0 : start / pageSize + 1;
	}

	public void setCurrPageNo(long currPageNo) {
		this.currPageNo = currPageNo;
	}

//	public int getStart() {
//		return totalCount > 0 ? 1 : 0;
//	}

	public void setTotalPageCount(long totalPageCount) {
		this.totalPageCount = totalPageCount;
	}

	public void setTotalCount(long totalCount) {
		this.totalCount = totalCount;
	}
}
